package com.nav.backend.mapper;

import com.nav.backend.pojo.User;
import com.nav.backend.pojo.UserAddress;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * UserMapper 接口用于定义对 User 表的数据库操作
 *
 * @author Seamher
 */
@Mapper
public interface UserMapper {

    /**
     * 通过用户名查找用户
     *
     * @param account 用户名
     * @return User 对象
     */
    @Select("select * from user where account = #{account}")
    User findByUserName(String account);

    /**
     * 通过邮箱查找用户
     *
     * @param email 用户邮箱
     * @return User 对象
     */
    @Select("select * from user where email = #{email}")
    User findByUserEmail(String email);

    /**
     * 通过手机号查找用户
     *
     * @param phone 用户手机号
     * @return User 对象
     */
    @Select("select * from user where phone = #{phone}")
    User findByUserPhone(String phone);

    /**
     * 插入新用户记录
     *
     * @param account  用户名
     * @param password 密码
     */
    @Insert("insert into user (account, password) values (#{account}, #{password})")
    void add(String account, String password);

    /**
     * 注销用户
     *
     * @param account 用户名
     */
    @Delete("delete from user where account = #{account}")
    void logout(String account);

    /**
     * 修改用户信息
     *
     * @param account    用户名
     * @param password   密码
     * @param phone      电话
     * @param email      邮箱
     * @param newAccount 新用户名
     */
    @Update("update user set account = #{newAccount}, password = #{password}, phone = #{phone}, email = #{email} where account = #{account}")
    void modifyInfo(String account, String password, String phone, String email, String newAccount);

    /**
     * 添加用户地址
     *
     * @param userComment    地址评论
     * @param userAddress    地址
     * @param userAddressX   地址 X 坐标
     * @param userAddressY   地址 Y 坐标
     * @param userAddressInfo 地址信息
     * @param account        用户名
     */
    @Insert("insert into user_address (user_comment, user_address, user_address_x, user_address_y, user_address_info, account) values (#{userComment}, #{userAddress}, #{userAddressX}, #{userAddressY}, #{userAddressInfo}, #{account})")
    void addAddress(String userComment, String userAddress, Double userAddressX, Double userAddressY, String userAddressInfo, String account);

    /**
     * 获取用户地址列表
     *
     * @param account 用户名
     * @return 用户地址列表
     */
    @Select("select * from user_address where account = #{account}")
    List<UserAddress> getAddress(String account);

    /**
     * 修改用户地址
     *
     * @param userComment    地址评论
     * @param userAddress    地址
     * @param userAddressX   地址 X 坐标
     * @param userAddressY   地址 Y 坐标
     * @param userAddressInfo 地址信息
     * @param userAddressID  地址 ID
     */
    @Update("update user_address set user_comment = #{userComment}, user_address = #{userAddress}, user_address_x = #{userAddressX}, user_address_y = #{userAddressY}, user_address_info = #{userAddressInfo} where user_address_id = #{userAddressID}")
    void modifyAddress(String userComment, String userAddress, Double userAddressX, Double userAddressY, String userAddressInfo, Integer userAddressID);

    /**
     * 删除用户地址
     *
     * @param userAddressId 地址 ID
     */
    @Delete("delete from user_address where user_address_id = #{userAddressId}")
    void deleteAddress(Integer userAddressId);

    @Delete("delete from user_address where account = #{account}")
    void logoutUserAddress(String account);

    @Delete("delete from collection_location where account = #{account}")
    void logoutCollectionAddress(String account);

    @Delete("delete from collection_path where account = #{account}")
    void logoutCollectionPath(String account);

    @Delete("delete from ring_search where account = #{account}")
    void logoutRingSearch(String account);

    @Delete("delete from tree_line_search where account = #{account}")
    void logoutTreeLineSearch(String account);

    @Delete("delete from two_points_search where account = #{account}")
    void logoutTwoPointsSearch(String account);
}
